package com.riko.comic.module.comic.service.impl;

import afu.org.checkerframework.checker.oigj.qual.O;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.riko.comic.module.comic.bean.po.ChapterImg;
import com.riko.comic.module.comic.bean.po.Comic;
import com.riko.comic.module.comic.mapper.ComicMapper;
import com.riko.comic.module.comic.mapper.TagMapper;
import com.riko.comic.module.comic.service.IComicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xt
 * @since 2021/11/01
 */
@Service
public class ComicServiceImpl extends ServiceImpl<ComicMapper, Comic> implements IComicService {

    @Autowired
    private ComicMapper comicMapper;

    @Autowired
    private TagMapper tagMapper;

    @Override
    public List<ChapterImg> queryChapterImg(String comicName, String number) {
        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("comicName",comicName);
        queryMap.put("number",number);

        return comicMapper.queryChapterImg(queryMap);
    }

    @Override
    public List<Comic> queryByTag(Integer tag, Integer index, Integer pageSize) {

        if(tag.equals(0)){
            return list(Wrappers.lambdaQuery(Comic.class).last("limit " + (index - 1) * pageSize + "," + pageSize));
        }

        Map<String,Object> queryMap = new HashMap<>();
        queryMap.put("tag",tag);
        queryMap.put("offset",(index-1) * pageSize);
        queryMap.put("pageSize",pageSize);

        return comicMapper.queryByTag(queryMap);
    }

    @Override
    public long queryCountByTag(Integer tag) {

        if(tag.equals(0)){
            return count();
        }

        return comicMapper.queryCountByTag(tag);
    }
}
